# -*- coding: UTF-8 -*-
import sys
import tkinter as tk
from tkinter import ttk
from Code.Utils.sc_func import Common, Singleton
from Code.View.Datamation.sc_provide import view_gate
from Code.View.sc_global import Global
from Code.View.sc_login import LoginApp
from Code.View.sc_main import MainApp
from Code.View.sc_module import WidgetTip, center_window


@Singleton
class AppGate(object):
    """ App代理 """

    def __init__(self):
        self.start_time = Common.time()
        self.apps = Apps()
        self.use_time = 0

    def run(self):
        self.apps.pack()
        center_window(self.apps)
        self.update_app_title(view_gate.query_app_title_data())
        self.use_time = Common.time() - self.start_time
        self.apps.mainloop()

    def close(self, ask=True):
        self.apps.close(ask)

    def switch_main(self):
        self.refresh()
        self.apps.withdraw()
        self.apps.main()
        self.apps.deiconify()
        center_window(self.apps)

    def refresh(self):
        Global.G_APP_WIDTH = w = self.apps.winfo_screenwidth()
        Global.G_APP_HEIGHT = h = self.apps.winfo_screenheight()
        if w >= 1600:
            Global.G_APP_WIDTH = 1600
        elif 1400 <= w < 1600:   # 老式显示器
            Global.G_APP_WIDTH = 1400
        elif 1300 <= w < 1400:   # 笔记本
            Global.G_APP_WIDTH = 1250
        if h >= 1000:
            Global.G_APP_HEIGHT = 950
        elif 800 <= h < 1000:
            Global.G_APP_HEIGHT = 750
        elif 750 <= h < 800:
            Global.G_APP_HEIGHT = 700
        Global.refresh()

    def resize(self, width, height):
        self.apps.geometry("{}x{}".format(width, height))

    def hide(self):
        self.apps.state('icon')

    def update_app_title(self, data):
        if not data:
            return
        self.apps.title(data)
        ico = view_gate.query_photo_image_data('ICO')
        if ico:
            self.apps.iconbitmap(ico)

    def update_app_trees(self, data):
        # TODO update_trees for new data
        pass

    def update_app_widgets(self, data):
        pass   # 界面控件重新进入界面即可刷新 #

    def update_try_login_return(self, data):
        if self.apps.login_app.try_login_result(data):
            self.switch_main()

    def update_new_login_return(self, data):
        if self.apps.main_app:
            self.apps.main_app.new_login_result(data)

    def update_login_state(self, data):
        if self.apps.main_app:
            self.apps.main_app.update_ip_state(data)

    def update_infotext_data(self, data):
        if self.apps.main_app:
            self.apps.main_app.update_infotext_data(data)

    def update_progress_data(self, data):
        if self.apps.main_app:
            self.apps.main_app.update_progress_data(data)

    def update_printout_data(self, data):
        if self.apps.main_app:
            self.apps.main_app.update_printout_data(data)

    def update_widgets_data(self, data):
        if self.apps.main_app:
            self.apps.main_app.update_widgets_data(data)

    def update_enter_exec_result(self, data):
        if self.apps.main_app:
            self.apps.main_app.execute_enter_result(data)

    @classmethod
    def single_start_result(cls, data):
        view_gate.single_owner.single_start_result(data)

    @classmethod
    def single_upload_result(cls, data):
        view_gate.single_owner.single_upload_result(data)

    @classmethod
    def single_download_result(cls, data):
        view_gate.single_owner.single_download_result(data)


class Apps(tk.Tk):
    """ 界面入口基类 """

    def __init__(self):
        tk.Tk.__init__(self)
        self.login_app = None
        self.main_app = None

    def theme_style(self):
        style = ttk.Style()
        style.theme_use('vista')  # vista  # clam
        style.configure("App.TEntry", background=Global.G_DEFAULT_COLOR)
        style.configure("App.TButton", borderwidth=0, relief='flat')
        style.configure("App.TNotebook", tabposition='wn', background=Global.G_DEFAULT_COLOR)
        style.configure("Settings.TButton", font=(Global.G_DEFAULT_FONT, 10))

    def pack(self):
        self.theme_style()
        self.login()
        self.attributes('-topmost', 1)
        self.protocol("WM_DELETE_WINDOW", self.close)
        self.resizable(False, False)

    def close(self, ask=True):
        if ask and not WidgetTip.ask('{} ?'.format(view_gate.query_language_data('CONFIRM', 'EXIT'))):
            return
        self.destroy()
        try:
            view_gate.app_close_data.set_data(True)
        finally:
            sys.exit(0)

    def login(self):
        """ 登录界面 """
        master = tk.Frame(self)
        master.pack()
        self.login_app = LoginApp(app_gate, master)
        self.login_app.pack()

    def main(self):
        self.attributes('-topmost', 0)
        master = tk.Frame(self)
        master.pack()
        menu = tk.Menu(self)
        self.config(menu=menu)
        self.main_app = MainApp(app_gate, menu, master)
        self.main_app.pack()


app_gate = AppGate()
